package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.mapper.HomeRecommendSubjectMapper;
import com.itheima.mall.pojo.HomeRecommendSubject;
import com.itheima.mall.service.SmsHomeRecommendSubjectService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsHomeRecommendSubjectServiceImpl extends ServiceImpl<HomeRecommendSubjectMapper, HomeRecommendSubject> implements SmsHomeRecommendSubjectService {

    @Autowired
    private HomeRecommendSubjectMapper homeRecommendSubjectMapper;

    /**
     * 添加首页推荐专题
     */
    @Override
    public void createSubject(List<HomeRecommendSubject> homeRecommendSubjectList) {
        if (CollUtil.isEmpty(homeRecommendSubjectList)){
            throw new BusinessException(ErrorResult.error());
        }
        homeRecommendSubjectList = homeRecommendSubjectList.stream().map(itme -> {
            itme.setSort(100);
            return itme;
        }).collect(Collectors.toList());
        this.saveBatch(homeRecommendSubjectList);
    }

    /**
     * 批量删除推荐
     */
    @Override
    public void deleteSubject(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new BusinessException(ErrorResult.error());
        }
        this.removeBatchByIds(ids);
    }

    /**
     * 分页查询推荐
     */
    @Override
    public PageResult listSubject(Integer pageNum, Integer pageSize, Integer recommendStatus, String subjectName) {
        Page<HomeRecommendSubject> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(subjectName != null, HomeRecommendSubject::getSubjectId,subjectName)
                .eq(recommendStatus != null,HomeRecommendSubject::getRecommendStatus,recommendStatus)
                .orderByAsc(HomeRecommendSubject::getSort);
        Page<HomeRecommendSubject> pageInfo = homeRecommendSubjectMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 批量修改推荐状态
     */
    @Override
    public void updateStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<HomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CollUtil.isNotEmpty(ids), HomeRecommendSubject::getId,ids)
                .set(recommendStatus != null,HomeRecommendSubject::getRecommendStatus,recommendStatus);
        homeRecommendSubjectMapper.update(null,updateWrapper);
    }

    /**
     * 修改推荐排序
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<HomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(HomeRecommendSubject::getSort,sort)
                .eq(HomeRecommendSubject::getId,id);
        homeRecommendSubjectMapper.update(null,updateWrapper);
    }


}
